var brand=[];
var model=[];
var series=[];
//layui时间选择器
laydate.render({
  elem: '.buy_date'
});
JSON.stringify()
laydate.render({
  elem: '.driving_license_issued'
});
laydate.render({
  elem: '.driving_license_effective'
});
laydate.render({
  elem: '.register_time'
  ,type: 'datetime'
});
var areaload= layer.load(0, {shade: false})
ajax(api.area.list,{
	is_all:1
},'post').done((res)=>{
	layer.close(areaload)
	var tree=[];
		tree=fortree(tree,res.data,1)
		function fortree(tree,data,type){
			for(var i=0;i<data.length;i++){
				var qz=''
				for(var ss=1;ss<type;ss++){
					qz+='├'
				}
				data[i].area_name2=qz+data[i].area_name
				tree.push(data[i])
				if(data[i].children.length>0){
					fortree(tree,data[i].children,type+1)
				}else{}
			}
			return tree
		}
		console.log(tree)
		var options=``;
		for(let i=0;i<tree.length;i++){
			options+=`<option value='${tree[i].area_id}'>${tree[i].area_name}</option>`;
		}
		$('.area_id').html(options)
}).fail(()=>{
	layer.close(areaload)
})


//edit
{if $Think.get.car_id}
ajax(api.car.list,{
	where_json:JSON.stringify({
		car_id:['in','{$Think.get.car_id}']}
	)},'post').done(function(cardetail){
		for(var i=0;i<cardetail.list.length;i++){
					cardetail.list[i].company_name=cardetail.list[i].company.company_name
					cardetail.list[i].company_id=cardetail.list[i].company.company_id
					cardetail.list[i].car_type_name=cardetail.list[i].cartype.car_type_name
					cardetail.list[i].entrust_company_name=cardetail.list[i].entrust.company_name
					cardetail.list[i].entrust_company_id=cardetail.list[i].entrust.company_id
			}
		console.log(cardetail)
		if(cardetail.list[0].entrust_company_id>0){
			$('.ziyou').val(2)
				$('.entrust').css('display','block')
		}
		$('input').each(function(){
			if($(this).data('check')){
				if(cardetail.list[0][$(this).attr('name')]){
					if(cardetail.list[0][$(this).attr('name')]=='0000-00-00'||cardetail.list[0][$(this).attr('name')]=="0000-00-00 00:00:00"){
						
					}else{
						$(this).val(cardetail.list[0][$(this).attr('name')])
					}
				}
				
			}
		})
		$('select').each(function(){
			if($(this).data('check')){
				if(cardetail.list[0][$(this).attr('name')]){
						$(this).val(cardetail.list[0][$(this).attr('name')])
				}
				
				
			}
		})
		$('img').each(function(){
			if($(this).data('key')){
				if(cardetail.list[0][$(this).data('key')]){
					$(this).attr('src',domain+'/'+cardetail.list[0][$(this).data('key')])
				}
			}
		})
{else/}
{/if}
//请求品牌
ajax(api.car.brands, {
	parent_id: 0
}, 'post').done(function(res) {
	brand=res.data
	
	{if $Think.get.car_id}
	brandselect('car_brand_id',res.data,cardetail.list[0].car_brand_id)
	{else/}
	brandselect('car_brand_id',res.data)
	if(brand.length>0){
	ajax(api.car.brands, {
		parent_id:brand[0].car_brand_id
	}, 'post').done(function(res2) {
		model=res2.data
		brandselect('car_model_id',res2.data)
		if(model.length>0){
			ajax(api.car.brands, {
				parent_id: model[0].car_brand_id
			}, 'post').done(function(res3) {
				series=res3.data
				brandselect('car_series_id',res3.data)
			}).fail()
		}
	}).fail()
	}
	{/if}
}).fail()
{if $Think.get.car_id}
	ajax(api.car.brands, {
		parent_id: cardetail.list[0].car_brand_id
	}, 'post').done(function(res2) {
		series=res2.data
		brandselect('car_model_id',res2.data,cardetail.list[0].car_model_id)
	}).fail()
	ajax(api.car.brands, {
		parent_id: cardetail.list[0].car_model_id
	}, 'post').done(function(res3) {
		series=res3.data
		brandselect('car_series_id',res3.data,cardetail.list[0].car_series_id)
	}).fail()
}).fail(function(){})
{else/}
{/if}



form.on('select(car_brand_id)', function(data) {
	Load()
	ajax(api.car.brands, {
		parent_id:data.value
	}, 'post').done(function(res2) {
		model=res2.data
		brandselect('car_model_id',res2.data)
		if(model.length>0){
			ajax(api.car.brands, {
				parent_id: model[0].car_brand_id
			}, 'post').done(function(res3) {
				Load(1)
				series=res3.data
				brandselect('car_series_id',res3.data)
			}).fail()
		}else{
			brandselect('car_series_id',res2.data)
			Load(1)
		}
	}).fail()
})
form.on('select(car_model_id)', function(data) {
	Load()
	ajax(api.car.brands, {
				parent_id:data.value
			}, 'post').done(function(res3) {
				Load(1)
				series=res3.data
				brandselect('car_series_id',res3.data)
			}).fail()
})


function brandselect(classname,res,id){
	var options=``
	if(res.length>0){
		for(var i=0;i<res.length;i++){
			if(id){
				if(res[i].car_brand_id==id){
					options+=`<option selected value='${res[i].car_brand_id}'>${res[i].name}</option>`
				}else{
					options+=`<option value='${res[i].car_brand_id}'>${res[i].name}</option>`
				}
			}else{
				options+=`<option value='${res[i].car_brand_id}'>${res[i].name}</option>`
			}
			
		}
	}else{
		options=`<option selected value='0'>无数据</option>`	
	}
	$("."+classname).html(options)
	form.render('select');
}

//请求区域列表
var entrust_company_name;
var entrust_company_id;
var data1={};
upImage(api.upimage.upimage, '#front_img', '#img1', data1,'front_img');
upImage(api.upimage.upimage, '#back_img', '#img2', data1,'back_img');
upImage(api.upimage.upimage, '#side_img', '#img3', data1,'side_img');
upImage(api.upimage.upimage, '#engine_img', '#img4', data1,'engine_img');
upImage(api.upimage.upimage, '#business_policy_img', '#img5', data1,'business_policy_img');
upImage(api.upimage.upimage, '#compulsory_policy_img', '#img6', data1,'compulsory_policy_img');
upImage(api.upimage.upimage, '#driving_license_img', '#img7', data1,'driving_license_img');
upImage(api.upimage.upimage, '#driving_license_img2', '#img8', data1,'driving_license_img2');

//ajax(api.area.list,{},'post'
//).done(function(res){
//	console.log(res)
//	var area_options='';
//	for(var i=0;i<res.list.length;i++){
//		if(res.list[i].area_pid>0){
//			res.list[i].area_name='--'+res.list[i].area_name
//		}
//		area_options+=`<option value='${res.list[i].area_id}'>${res.list[i].area_name}</option>`
//	}
//	$('.area_id').html(area_options)
//	form.render('select');
//}).fail(function(){
//	
//})
$('.company_name').on('click',function(){
	opsearch("{:url('html/system/company')}?op=search")
});
$('.entrust_company_name').on('click',function(){
	opsearch("{:url('html/system/company')}?op=entrust")
});
$('.car_type_name').on('click',function(){
	opsearch("{:url('html/car/models')}?op=search")
});
form.on('select(ziyou)', function(data) {
	if(data.value==1){
		entrust_company_name=$('.entrust_company_name').val()
		entrust_company_id=$('.entrust_company_id').val()
		$('.entrust').css('display','none')
		$('.entrust_company_name').val("")
		$('.entrust_company_id').val("")
	}else{
		$('.entrust').css('display','block')
		if(entrust_company_name){
			$('.entrust_company_name').val(entrust_company_name)
			$('.entrust_company_id').val(entrust_company_id)
		}
	}
})
form.on('submit(ss2)', function(data) {
	Load()
				console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
				console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
				console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
				console.log()
				for(var key in data1){
					data.field[key]=data1[key]
				}
				{if $Think.get.car_id}
				var sumbit=api.car.edit
				data.field.car_id='{$Think.get.car_id}'
				{else/}
				var sumbit=api.car.add
				{/if}
				console.log('{$Think.get.car_id}')
				ajax(sumbit, data.field, 'post').done(function(res) {
					console.log(res)
					if(res.code==1){
							Load(1)
							Msg('提交成功')
							{if $Think.get.car_id}
								window.history.go(0)
							{else/}
								window.location.href=path.car.index
							{/if}
					}else{
						Err(res.msg)
					}
				}).fail(function(err) {
						Err('请求失败')
				});

				return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
})